﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using P2PWeb.Common;
using P2PWeb.Models;
using Nectar.DAL.Common;

namespace P2PWeb.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        // 注册导航页
        public ActionResult RegisterNav()
        {
            return View();
        }
        // 注册
        public ActionResult Register()
        {
            string tab = Request.QueryString["tab"];
            return View();
        }
        //注册协议
        public ActionResult Argument()
        {
            return View();
        }

        // 提交注册表单
        public ActionResult GetRegisterInfo()
        {
            using (var context = new Repository<P2PModelContainer>())
            {
                Int64 mobile;
                Account ac = new Account();
                try
                {
                    if (Request["Mobile"].IndexOf('@') == -1)
                    {
                        mobile = Int64.Parse(Request["Mobile"]);
                        ac.Mobile = mobile.ToString();
                    }
                    else
                    {
                        ac.Email = Request["Mobile"];
                    }

                }
                catch (Exception)
                {

                }
                ac.Name = Request["Name"];
                ac.PassWord = Request["PassWord"];
                ac.CreationTime = DateTime.Now;
                ac.UpdateTime = DateTime.Now;
                ac.Status = 0;
                ac.Type = 1;
                context.Add<Account>(ac);
            }
            return RedirectToAction("SignIn", "Login");
        }

        public string checkUser()
        {
            try
            {
                if (!string.IsNullOrEmpty(Request["s_mobile_account"]))
                {
                    using (var context = new Repository<P2PModelContainer>())
                    {
                        List<QueryParameter> parameters = new List<QueryParameter>();
                        parameters.Add(new QueryParameter { Value = Request["s_mobile_account"], Name = "Mobile" });

                        var query = context.GetListOf<Account>("o.Mobile = @Mobile", parameters.ToArray());
                        if (query.Count == 1)
                        {
                            return "6";
                        }
                    }
                }
                if (!string.IsNullOrEmpty(Request["email"]))
                {
                    using (var context = new Repository<P2PModelContainer>())
                    {
                        List<QueryParameter> parameters = new List<QueryParameter>();
                        parameters.Add(new QueryParameter { Value = Request["email"], Name = "email" });
                        var query = context.GetListOf<Account>("o.Email = @email", parameters.ToArray());
                        if (query.Count == 1)
                        {
                            return "2";
                        }
                    }

                }
                if (!string.IsNullOrEmpty(Request["s_cnname"]))
                {
                    using (var context = new Repository<P2PModelContainer>())
                    {
                        List<QueryParameter> parameters = new List<QueryParameter>();
                        parameters.Add(new QueryParameter { Value = Request["s_cnname"], Name = "s_cnname" });

                        var query = context.GetListOf<Account>("o.Name = @s_cnname", parameters.ToArray());
                        if (query.Count == 1)
                        {
                            return "1";
                        }
                    }
                }
                return "9";
            }
            catch (Exception)
            {
                return "9";
            }
        }


        //登录
        public ActionResult SignIn(string rawurl)
        {
            ViewData["backUrl"] = rawurl;
            return View();
        }

        public ActionResult SignOut()
        {
            Session["UserInfo"] = null;
            return Redirect("SignIn");
        }


        private string validate(string name,string pwd,string srand,string Code)
        {
            string result = string.Empty; ;
            if (name.Trim().Equals(""))
                result = "请输入姓名";
            else if (pwd.Trim().Equals(""))
                result = "请输入密码";
            else if (srand.Trim().Equals(""))
                result = "请输入验证码";
            else if (!srand.ToLower().Equals(Code.ToLower()))
                result = "验证码错误！";

            return result;
        }
        //验证是否可以通过
        [HttpPost()]
        public string ValidateForm()
        {
            string result = string.Empty;
            string name = Request["Name"];
            string pwd = Request["PassWord"];
            string srand = Request["srand"];
            string Code = Session["LoginCheckCode"].ToString();

            //Repository<Account> account=new Repository<Account>();
            result = validate(name, pwd, srand, Code);
            if (string.IsNullOrEmpty(result))
            {
                using (var context = new Repository<P2PModelContainer>())
                {
                    List<QueryParameter> parameters = new List<QueryParameter>();
                    parameters.Add(new QueryParameter { Value = name, Name = "ID" });
                    parameters.Add(new QueryParameter { Value = pwd, Name = "password" });
                    List<Account> query = context.GetListOf<Account>("(o.Mobile = @ID or o.Email = @ID) and o.PassWord = @password", parameters.ToArray());

                    if (query.Count != 0)
                    {
                        Session["UserInfo"] = query.First();
                        return "sucess";
                    }
                    else
                    {
                        result = "用户名或密码错误！";
                        //return RedirectToAction("Index");
                    }
                }
            }
            //return RedirectToAction("Index", "MyBorrows");
            return result;
        }

        // 验证码
        public ActionResult ValidateCode()
        {
            ValidateCodeHelper validateCode = new ValidateCodeHelper();
            byte[] bytes = validateCode.CreateImage();
            Session["LoginCheckCode"] = validateCode.RandomCode;
            return File(bytes, @"image/gif");
        }

        // 忘记密码
        public ActionResult ForgetPassword()
        {
            return View();
        }

        //计算器数据
        public ActionResult Calculator()
        {
            return View();
        }

        //Ajax请求计算器数据
        public ActionResult CalculatorShow()
        {
            //借款总额
            int Principal = 10000;// Request.QueryString["Principal"];
            //还款期限
            int Period = 12;// Request.QueryString["Period"];
            //利息
            double Interest = 0.1;
            //平台费
            double Cost = 33;


            return PartialView("CalculatorPartial");
        }
    }
}
